package com.applink.sell_proof.database.table;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.NewRewardProjectDetail;
import com.applink.sell_proof.pojo.GenericAttribute;
import com.applink.sell_proof.pojo.GenericObject;
import com.applink.sell_proof.share.Parameter;
import com.applink.sell_proof.utilXMLParser.ObjPropInfo;

public class RewardProjectDetailDBHelper {
	public static final String TABLE_NAME = "reward_project_detail_table";
	
	public static final String RW_PG_OBJECT_ID = "ObjectID";
	public static final String RW_PG_REWARD_TYPE = "RewardType";
	public static final String RW_PG_REWARD_AMOUNT = "Amount";
	public static final String RW_PG_CUSTOMER_COMPANY = "CustomerCompany";
	public static final String RW_PG_OPPORTUNITY_NAME = "OpportunityName";
	public static final String RW_PG_INVOICE_DATE = "InvoiceDate";
	public static final String RW_CL_DT_INVOICE_NO = "InvoiceNo";
	
	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ RW_PG_OBJECT_ID + " integer primary key ," 
			+ RW_PG_REWARD_TYPE + " text, " 
			+ RW_PG_REWARD_AMOUNT + " text ," 
			+ RW_PG_CUSTOMER_COMPANY + " text ,"
			+ RW_PG_OPPORTUNITY_NAME + " text ," 
			+ RW_PG_INVOICE_DATE + " text ," 
			+ RW_CL_DT_INVOICE_NO + " text " + ");";
	
	public static void createProjectDetail(final NewRewardProjectDetail projectDetail) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(RW_PG_OBJECT_ID, projectDetail.getObjectID());
				value.put(RW_PG_REWARD_TYPE, projectDetail.getRewardType());
				value.put(RW_PG_REWARD_AMOUNT, projectDetail.getAmount());
				value.put(RW_PG_CUSTOMER_COMPANY, projectDetail.getCustomerCompany());	
				value.put(RW_PG_OPPORTUNITY_NAME, projectDetail.getOpportunityName());
				value.put(RW_PG_INVOICE_DATE, projectDetail.getInvoiceDate());
				value.put(RW_CL_DT_INVOICE_NO, projectDetail.getInvoiceNo());
				resolver.insert(EMarketingContentProvider.REWARD_PROJECT_DETAIL_CONTENT_URI,
						value);
			}

		}.start();
	}	
	
	public static void clearProjectDetail(){
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.delete(EMarketingContentProvider.REWARD_PROJECT_DETAIL_CONTENT_URI, null, null);
	}
	
	@SuppressWarnings("rawtypes")
	public static ArrayList<GenericObject> getObjectBasedConfig(ArrayList<ObjPropInfo> objPropList) {
		
		if(objPropList.size() ==0){
			return null;
		}
		
		ArrayList<GenericObject> genericObjectList = new ArrayList<GenericObject>();
		  			
	        try {
	        	//whole items in objPropList must have the same om_id since they belong to an unique Generic Object
	        	//at here we fetch data of each record based on ID of each property
	        	
	        	ContentResolver resolver = EMarketing.getInstance().getApplicationContext().getContentResolver();
	        
	        	String [] queriedColumns = new String [objPropList.size()];
	        	for (int i=0;i < objPropList.size();i++)
	        		queriedColumns[i] = objPropList.get(i).getOPID();

	        	Cursor cursor = resolver.query(
	        			EMarketingContentProvider.REWARD_PROJECT_DETAIL_CONTENT_URI, queriedColumns, null,
	    				null, null);
	        	
	            if( cursor !=null) {

	            	while(cursor.moveToNext()){	
	            		
	            		//create new genericObject
	            		GenericObject genericObject = new GenericObject();
	            		genericObject.setObjectName(objPropList.get(0).getOm());
	            		
	            		ArrayList<GenericAttribute> attributeList = new ArrayList<GenericAttribute>();
                		
	            		for(int j=0; j < objPropList.size() ; j++){ //the number of attributes of the record must be the same with objPropList's size
	                		
	            			ObjPropInfo objPropInfo = objPropList.get(j);
	                		
	                		String name = objPropInfo.getOPID();
	                		String dataType = objPropInfo.getDataType();
	                			                		
	                		if(dataType.compareTo(Parameter.TYPE_INT) == 0) {
	                			GenericAttribute<String, Integer> attribute = new GenericAttribute<String, Integer>(name, (int) cursor.getLong(cursor.getColumnIndex(name)));
	                			attributeList.add(attribute);
	                		} else if (dataType.compareTo(Parameter.TYPE_CHAR) == 0){
	                			GenericAttribute<String, String> attribute = new GenericAttribute<String, String>(name, cursor.getString(cursor.getColumnIndex(name)));
	                			attributeList.add(attribute);
	                		} else if (dataType.compareTo(Parameter.TYPE_DOUBLE) == 0){
	                			GenericAttribute<String, Double> attribute = new GenericAttribute<String, Double>(name, (double)cursor.getDouble(cursor.getColumnIndex(name)));           			
	                			attributeList.add(attribute);
	                		}
	                		//if you have more datatype, please define here
	                	}
	            		
	            		genericObject.setAttributeList(attributeList);
	            		genericObjectList.add(genericObject);	                

	            	}
	            }
	        } catch (SQLException e) {
	        	genericObjectList = null;
	        }
   	
    	return genericObjectList;
	}
	
	@SuppressWarnings("rawtypes") //this function is just used for Outlet since it considers on KeyID
	public static ArrayList<GenericObject> loadObjectListByID(int objectID, ArrayList<ObjPropInfo> objPropList) {
	    
		if(objPropList.size() ==0){
			return null;
		}
		
		ArrayList<GenericObject> genericObjectList = new ArrayList<GenericObject>(); 
		try {	
	    	//whole items in objPropList must have the same om_id since they belong to an unique Generic Object
        	//at here we fetch data of each record based on ID of each property
		  	ContentResolver resolver = EMarketing.getInstance().getApplicationContext().getContentResolver();
		       
        	String [] queriedColumns = new String [objPropList.size()];
        	for (int i=0;i < objPropList.size();i++)
        		queriedColumns[i] = objPropList.get(i).getOPID();
        
        	Cursor cursor = resolver.query(
        			EMarketingContentProvider.REWARD_PROJECT_DETAIL_CONTENT_URI, queriedColumns,  RW_PG_OBJECT_ID + "=" + objectID,null, null);
        	
        	if( cursor !=null) {  
        		
        		while(cursor.moveToNext()){	  
        			
        			//create new genericObject
            		GenericObject genericObject = new GenericObject();
            		genericObject.setObjectName(objPropList.get(0).getOm());
            		
            		ArrayList<GenericAttribute> attributeList = new ArrayList<GenericAttribute>();
            	
            		for(int j=0; j < objPropList.size() ; j++){ //the number of attributes of the record must be the same with objPropList's size
            		
            			ObjPropInfo objPropInfo = objPropList.get(j);
            		
            			String name = objPropInfo.getOPID();
            			String dataType = objPropInfo.getDataType();
            			                		
            		
            			if(dataType.compareTo(Parameter.TYPE_INT) == 0) {
            				GenericAttribute<String, Integer> attribute = new GenericAttribute<String, Integer>(name, (int) cursor.getLong(cursor.getColumnIndex(name)));
            				attributeList.add(attribute);
            			} else if (dataType.compareTo(Parameter.TYPE_CHAR) == 0){
            				GenericAttribute<String, String> attribute = new GenericAttribute<String, String>(name, cursor.getString(cursor.getColumnIndex(name)));
            				attributeList.add(attribute);
            			} else if (dataType.compareTo(Parameter.TYPE_DOUBLE) == 0){
            				GenericAttribute<String, Double> attribute = new GenericAttribute<String, Double>(name, (double)cursor.getDouble(cursor.getColumnIndex(name)));           			
            				attributeList.add(attribute);
            			}
            			//if you have more datatype, please define here
            		}
        		
            		genericObject.setAttributeList(attributeList);
            		genericObjectList.add(genericObject);	  
        		}
        	}
		} catch(SQLException e){
			genericObjectList = null;
	    } 
		
	    return genericObjectList;
	}
}
